
#ifndef __BSP_UART_H__
#define __BSP_UART_H__

#include <stdbool.h>
#include <stdint.h>
#include "lrb.h"

#define UART_PORT_LIST(M) \
    M(UART_PORT0) \
    M(UART_PORT1) \
    M(UART_PORT2)

#define UART_PORT_ENUM(e) e,
typedef enum
{
	UART_PORT_LIST(UART_PORT_ENUM)
    UART_PORT_MAX
} uart_port_e;

typedef enum
{
	UART_WORDLENGTH_8,
	UART_WORDLENGTH_9
} uart_wordlength_e;

typedef enum
{
	UART_STOPBITS_0_5,
	UART_STOPBITS_1,
	UART_STOPBITS_1_5,
	UART_STOPBITS_2
} uart_stopbits_e;

typedef enum
{
	UART_PARITY_NO,
	UART_PARITY_EVEN,
	UART_PARITY_ODD
} uart_parity_e;

typedef enum
{
	UART_MODE_TX = 0x1,
	UART_MODE_RX = 0x2,
	UART_MODE_RX_TX = 0x3
} uart_mode_e;
	
typedef enum
{
	UART_HWFC_None = 0x0,
	UART_HWFC_RTS,
	UART_HWFC_CTS,
	UART_HWFC_RTS_CTS
} uart_hardware_flowcontrol_e;

typedef struct
{
	uint32_t baud_rate;
	uart_wordlength_e word_length;
	uart_stopbits_e stop_bits;
	uart_parity_e parity;
	uart_mode_e mode;
	uart_hardware_flowcontrol_e hardware_flowcontrol;
    uint32_t rx_buffer_size;
} uart_cfg_t;

typedef struct
{
	uart_port_e port;
    int rx_rb_hdl;
    int rx_sem;
    uart_cfg_t cfg;
	int(*open)(uart_cfg_t *);
	int(*write)(uint8_t *, int);
	int(*read)(uint8_t *, int, uint32_t);
	void(*close)(void);
	void(*deinit)(void);
} uart_t;

extern uart_t *uart_init(uart_port_e port);
extern uart_t *uart_find_by_port(uart_port_e port);

#endif

